package jee.boot.entity.bpm;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;
import jee.boot.common.basic.DataEntity;
import jee.boot.common.utils.DateUtils;
import jee.boot.common.utils.JsonMapper;
import jee.boot.common.utils.MapUtils;
import jee.boot.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 表单设计_业务实体表
 */
@Table(name = "bpm_business_table")
public class BusinessTable extends DataEntity<BusinessTable> {
    private static final long serialVersionUID = 1L;
    /**
     * 别名
     */
    private String alias;
    /**
     * 表名
     */
    private String name;
    /**
     * 备注
     */
    private String comment;
    /**
     * 所有列信息
     */
    private String columnJson;
    /**
     * 是否外部表 字典: yes_no
     * 外部表是以数据库表生成的businessTable数据(数据库字段等信息只读)
     * 内部表是以businessTable生成的数据库表(数据库可以被同步修改)
     */
    private String external;
    /**
     * 所有的列
     */
    @Transient
    private List<Map<String, Object>> columnList = Lists.newArrayList();

    public BusinessTable() {
        setCacheKey(getProjectConfig().getName() + "_businessTableCache");
    }

    public BusinessTable(String id) {
        super(id);
        setCacheKey(getProjectConfig().getName() + "_businessTableCache");
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<Map<String, Object>> getColumnList() {
        if (columnList.isEmpty() && StringUtils.isNotBlank(columnJson)) {
            columnList = JsonMapper.fromJsonString(columnJson, List.class);
        }
        return columnList;
    }

    public void setColumnList(List<Map<String, Object>> columnList) {
        this.columnList = columnList;
        if (columnList != null && !columnList.isEmpty()) {
            columnJson = JsonMapper.toJsonString(columnList);
        }
    }

    @JsonIgnore
    public String getColumnJson() {
        return columnJson;
    }

    public void setColumnJson(String columnJson) {
        this.columnJson = columnJson;
    }

    public String getExternal() {
        return external;
    }

    public void setExternal(String external) {
        this.external = external;
    }

    /**
     * 转换之后的列
     * @return
     */
    public List<BusinessColumn> coverColumnList() {
        List<BusinessColumn> columns = Lists.newArrayList();
        for (Map<String, Object> map : getColumnList()) {
            columns.add(MapUtils.toBean(map, BusinessColumn.class));
        }
        return columns;
    }

    public static void main(String[] args) {
        String json="";
        new JsonMapper().enableSimple().fromJson(json,List.class);
    }
}
